package jee.boot.module.authority.service;

import jee.boot.common.basic.CrudService;
import jee.boot.common.basic.Page;
import jee.boot.common.basic.ServiceException;
import jee.boot.common.basic.result.MapData;
import jee.boot.entity.system.Filter;
import jee.boot.common.standard.AuthorityHandler;
import jee.boot.module.authority.dao.CellSetDao;
import jee.boot.module.authority.dao.ColumnDao;
import jee.boot.module.authority.dao.ModelDataDao;
import jee.boot.module.authority.dao.RowSetDao;
import jee.boot.entity.auth.CellSet;
import jee.boot.entity.auth.Column;
import jee.boot.entity.auth.ModelData;
import jee.boot.entity.auth.RowSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 模型
 */
@Service
public class ModelDataService extends CrudService<ModelDataDao, ModelData> {
    @Autowired
    private ColumnDao columnDao;
    @Autowired
    private RowSetDao rowSetDao;
    @Autowired
    private CellSetDao cellSetDao;
    @Autowired
    private RowSetService rowSetService;

    @Override
    public ModelData get(ModelData entity) {
        entity = super.get(entity);
        getMany(entity);
        return entity;
    }

    public void getMany(ModelData entity) {
        Column column = new Column();
        column.setPage(new Page<>());
        column.getPage().setOrderBy("sort");
        column.getPage().setOrderSort("asc");
        entity.setColumnList(super.getOneToMany(columnDao, entity, column, "modelData"));
        List<RowSet> rowSetList = super.getOneToMany(rowSetDao, entity, new RowSet(), "modelData");
        if(!rowSetList.isEmpty()){
            for(RowSet rowSet:rowSetList){
                rowSetService.getMany(rowSet);
            }
        }
        entity.setRowSetList(rowSetList);
        entity.setCellSetList(super.getOneToMany(cellSetDao,entity,new CellSet(),"modelData"));

    }

    @Override
    public void save(ModelData entity) {
        super.save(entity);
        super.saveOneToMany(columnDao, entity, "modelData", entity.getColumnList());
        super.saveOneToMany(rowSetDao,entity,"modelData",entity.getRowSetList());
        super.saveOneToMany(cellSetDao,entity,"modelData",entity.getCellSetList());
    }

    @Caching(evict = {
            @CacheEvict(value = AuthorityHandler.USER_AUTHORITY_KEY, allEntries = true)
    })
    @Override
    public void update(ModelData entity) {
        super.update(entity);
        super.updateOneToMany(columnDao, entity, new Column(), "modelData", entity.getColumnList());
        super.updateOneToMany(rowSetDao, entity, new RowSet(), "modelData", entity.getRowSetList());
        super.updateOneToMany(cellSetDao, entity, new CellSet(), "modelData", entity.getCellSetList());
    }

    @Caching(evict = {
            @CacheEvict(value = AuthorityHandler.USER_AUTHORITY_KEY, allEntries = true)
    })
    @Override
    public void delete(ModelData entity) {
        super.delete(entity);
        super.deleteOneToMany(columnDao, entity, new Column(), "modelData");
        super.deleteOneToMany(rowSetDao, entity, new RowSet(), "modelData");
        super.deleteOneToMany(cellSetDao, entity, new CellSet(), "modelData");
    }
    /**
     * 按模型名称查询模型定义
     *
     * @param entity
     * @return
     */
    public ModelData getModelDataByCode(ModelData entity) {
        ModelData query = new ModelData();
        query.addFilter(new Filter("code", "=", entity.getCode()));
        List<ModelData> list = findList(query);
        if (list == null || list.isEmpty()) {
            throw new ServiceException(1201, MapData.instance().put("code",entity.getCode()).data());
        }
        if (list.size() > 1) {
            throw new ServiceException(1202, MapData.instance().put("code",entity.getCode()).data());
        }
        ModelData model = list.get(0);
        Column column = new Column();
        column.addFilter(new Filter("modelData", "=", model.getId()));
        List<Column> columnList = columnDao.findList(column);
        model.setColumnList(columnList);
        return model;
    }

}
